package com.yanqu.road.dao.impl.activity.huashigang;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangTreasure;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/10/11
 */
public class HuaShiGangTreasureDaoImpl extends TempDataObjectDao<HuaShiGangTreasure> {
    public static String getHuaShiGangTreasureSql = "select * from t_c_huashigang_treasure where activityId=? order by treasureId asc";

    public Map<String, List<HuaShiGangTreasure>> getHuaShiGangTreasure(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, List<HuaShiGangTreasure>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getHuaShiGangTreasureSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HuaShiGangTreasure huaShiGangTreasure = parse(rs);
                if (!map.containsKey(huaShiGangTreasure.getPointKey())) {
                    map.put(huaShiGangTreasure.getPointKey(), new ArrayList<>());
                }
                map.get(huaShiGangTreasure.getPointKey()).add(huaShiGangTreasure);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateHuaShiGangTreasureSql = "update t_c_huashigang_treasure set goodReward=?,reward=?,openPlayer=?,openTime=? where activityId=? and pointKey=? and treasureId=?";

    @Override
    protected boolean updateData(HuaShiGangTreasure huaShiGangTreasure) {
        huaShiGangTreasure.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BOOLEAN, huaShiGangTreasure.isGoodReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangTreasure.getReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(huaShiGangTreasure.getOpenPlayer())));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangTreasure.getOpenTime()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangTreasure.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangTreasure.getPointKey()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangTreasure.getTreasureId()));
        if (getDb().executeNoneQuery(updateHuaShiGangTreasureSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHuaShiGangTreasureSql = "insert into t_c_huashigang_treasure(activityId,pointKey,treasureId,goodReward,reward,openPlayer,openTime) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(HuaShiGangTreasure huaShiGangTreasure) {
        huaShiGangTreasure.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangTreasure.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangTreasure.getPointKey()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangTreasure.getTreasureId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, huaShiGangTreasure.isGoodReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, huaShiGangTreasure.getReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(huaShiGangTreasure.getOpenPlayer())));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangTreasure.getOpenTime()));
        if (getDb().executeNoneQuery(insertHuaShiGangTreasureSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HuaShiGangTreasure parseData(ResultSet rs) throws SQLException {
        HuaShiGangTreasure huaShiGangTreasure = new HuaShiGangTreasure();
        huaShiGangTreasure.setActivityId(rs.getInt("activityId"));
        huaShiGangTreasure.setPointKey(rs.getString("pointKey"));
        huaShiGangTreasure.setTreasureId(rs.getInt("treasureId"));
        huaShiGangTreasure.setGoodReward(rs.getBoolean("goodReward"));
        huaShiGangTreasure.setReward(rs.getString("reward"));
        huaShiGangTreasure.setOpenPlayer(JSONObject.parseObject(rs.getString("openPlayer"), UserBaseInfo.class));
        huaShiGangTreasure.setOpenTime(rs.getLong("openTime"));

        return huaShiGangTreasure;
    }
}
